#include "mythread.h"



#define wait1 1000
#define SLEEP(t)  emit set_count_down_timer(t);for(uint it=0;it<t;it++){msleep(1000);_EXIT_ }
#define SLEEP_return_bool(t)  emit set_count_down_timer(t);for(uint i=0;i<t;i++){msleep(1000);_EXIT_0 }


mythread::mythread(QObject *parent) :
    QThread(parent)
{
    timer=new QTimer();
    QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
    delay_heat_timer=new QTimer();
    QObject:: connect(  delay_heat_timer, SIGNAL(timeout()), SLOT(condition_heating()) );
    delay_heat_timer->setSingleShot(true);
    F0_timer=new QTimer();
    QObject:: connect( F0_timer, SIGNAL(timeout()), SLOT(calculate_F0()) );
    stop_thread=0;
    rec=new recorder;
    track=new tracker;
    period_heat_time=10;
    bPSU=0;
    hTem=0;
    lTem=0;
}


void mythread::run()
{

      sterilizing=1;
      if(sterilization_protype == LEAK_TEST_PROTYPE)
     {
       leak_test();
     }
      else  if(sterilization_protype == DRY_PROTYPE)
      {
          exhaust_valve_OFF
          timer->start(30000);
      }
      else
      {
     readdata();
     F0=0;
     pressure_balanced=0;
     START_AUTO_HEAT
      if( !continuous_mode)//连续模式跳过预热过程
     {

     emit set_status_display(0);

   while( !inter_pressure_switch_isreached)
   {
         msleep(1000);
          _EXIT_
   }
      }

     low_temperature_counter=0;
     if(whole_machine_parameter.IO_tracker) track->start_track();
     exhaust_valve_OFF

     if(stype->is_pulsing_vacuum)
   {
       pulsing_vacuum();
    }
    else
    {      
       bottom_exhaust();
    }
      sterilizing=0;
     if(whole_machine_parameter.IO_tracker) track->stop_track();

    }
}


 void mythread::pulsing_vacuum()
 {
    int i;

            air_supply_valve_OFF;//避免上次回气未关就进行下一次
             /*************脉动阶段******************/
             //DISPLAY(1)
             STOP_AUTO_HEAT
             emit set_status_display(1);
             for( i=0;i<pulsing_times;i++)
           {
           emit set_status_display(i+101);
           Vacuum_pump_ON;
        loop1:
        while(PSU>pulsing_lowest_pressure){ msleep(wait1); _EXIT_ }
           msleep(wait1);
        if(PSU>pulsing_lowest_pressure) { goto loop1;}
           rec->record();
           Vacuum_pump_OFF;
           SLEEP(12)
           air_intake_valve_ON;
        //   heating_valve_ON
           delay_heating( heat_delay_time ,true );
        loop2:
        while(PSU<pulsing_highest_pressure)
        {
            if(TEM>sterilization_temperature -2) { air_intake_valve_OFF }
            else { if(TEM<sterilization_temperature -3) { air_intake_valve_ON }}
            msleep(wait1);
            _EXIT_
        }
           msleep(wait1);
        if(PSU<pulsing_highest_pressure) { goto loop2;}
             rec->record();
             timer->stop();
            air_intake_valve_OFF;
            _EXIT_
            msleep(wait1);
            }
              Vacuum_pump_ON;
        loop5:
        while(PSU>pulsing_lowest_pressure){ msleep(wait1);_EXIT_}
             msleep(wait1);
        if(PSU>pulsing_lowest_pressure) { goto loop5;}
              rec->record();
              Vacuum_pump_OFF;
          /*************升温阶段 1******************/
            // DISPLAY(2)
            if(!sterilization_process()) return;
            emit set_status_display(4);
        loop7:
        while(PSU>70){ msleep(wait1); exhaust_valve_ON;_EXIT_ }
           msleep(wait1);
        if(PSU>70) {goto loop7;}
             exhaust_valve_OFF;

                 for( i=0;i<dry_times ;i++)
                 {
                  rec->record();
                     Vacuum_pump_ON;
        loop8:
            while(PSU>dry_pressure){ msleep(wait1);_EXIT_}
                 msleep(wait1);
            if(PSU>dry_pressure) { goto loop8;}
                Vacuum_pump_OFF;
                rec->record();
              // if(sterilization_protype==BD_TEST_PROTYPE)  break;//BD实验结束
                //SET_TIMER(dry_keep_time)
                SLEEP(dry_keep_time)
                rec->record();
                if(i < (dry_times-1))
             {

                    air_supply_valve_ON;
        loop9:
        while(PSU<-10){ msleep(wait1);_EXIT_}
           msleep(wait1);
        if(PSU<-10) goto loop9;
                    air_supply_valve_OFF;

              }

                 }
                 emit set_status_display(5);  // DISPLAY(5)


 }



 void mythread::bottom_exhaust()
{

   emit set_status_display(1);
   air_intake_valve_ON
   exhaust_valve_ON
   emit set_count_down_timer(60);
   for(int i0=0;i0<60;i0++)
   {
       msleep(1000);    _EXIT_
       if(TEM>sterilization_temperature-2)
       {
           air_intake_valve_OFF
           break;
       }
   }
   heating_valve_ON
   air_intake_valve_OFF
   while(PSU>5) { msleep(1000);  _EXIT_}
   STOP_AUTO_HEAT
   for(int i2=0;i2< cold_exhaust_times;i2++)//排冷
   {
   emit set_status_display(i2+101);
   rec->record();
   air_intake_valve_ON;
   exhaust_valve_OFF
    heating_valve_OFF

   if(whole_machine_parameter.cold_exhaust_control_type)
 {


    delay_heating( heat_delay_time,true);
    while( PSU <cold_exhaust_pressure ) {msleep(500);_EXIT_}
   rec->record();

   air_intake_valve_OFF
   exhaust_valve_ON
   while(PSU> 5) {_EXIT_ msleep(1000);}
   exhaust_valve_OFF;
   }
   else
   {

         START_AUTO_HEAT
         emit set_count_down_timer(120);
         //timer->start(30000);
         for(int i1=0;i1<120;i1++)
         {
         msleep(1000);
         if(TEM>sterilization_temperature -2) {air_intake_valve_OFF; exhaust_valve_ON}
         else {if(TEM<sterilization_temperature -2) {air_intake_valve_ON ; exhaust_valve_OFF}}
         _EXIT_
         }
          //timer->stop();
          air_intake_valve_OFF
          exhaust_valve_ON
          SLEEP(100)             
          exhaust_valve_OFF;
   }

   }


   if(sterilization_protype ==  LIQUID_RPOTYPE) { F0_timer->start(6000);   }
   if(!sterilization_process()) return;
   emit set_status_display(4);  // DISPLAY(4)
   while(PSU>5) { msleep(500);_EXIT_}
   emit set_status_display(5);  // DISPLAY(5)

}



 void mythread::leak_test()
 {
    int t1=600;
    int t2=600;

    Vacuum_pump_ON
 loop1:
 while(PSU>-93){ msleep(1000);_EXIT_}
    msleep(1000);
 if(PSU>-93) { goto loop1;}
    Vacuum_pump_OFF;
    //test->timer_start();
    emit leak_test_timer_on();
    while(t1--)
    {
    _EXIT_
    msleep(1000);
    }
    pressure[1]=PSU;
    while(t2--)
    {
    _EXIT_
    msleep(1000);
    if(PSU - pressure[1] > 3) break;
    }
    pressure[2]=PSU;
     sterilizing=0;
    if(pressure[2]- pressure[1] < 3)  WARN->show_text("本次测试合格！");
    else    WARN->show_text("本次测试不合格！");

  }

void mythread::low_temperature_process()
{

    if(TEM<sterilization_temperature)
    {
       // count+=2;
       if(low_temperature_counter++ > sterilization_time*0.1)
       {
         //sterilization_stage=10;
         breakdown=LOW_TEM;
         STOP_CMD
         emit set_status_display(10);
         WARN->show_sound("温度低于预设温度，本次灭菌失败！");
       }
    }
//    else count--;
}

//void mythread::exception_process(uchar type)

void mythread::readdata()
{
    high_balance_temperature=(float)sensor_adjust_datamap[trUtf8("恒温上限")]*0.1;
    low_balance_temperature=(float)sensor_adjust_datamap[trUtf8("恒温下限")]*0.1;
    if(high_balance_temperature<low_balance_temperature)
    {
        WARN->show_sound("恒温设置错误：恒温上限低于恒温下限");
        STOP_CMD
    }

}
 void mythread::dry()
 {

    if(PSU > -60)
        {
        if( !inter_pressure_switch_isreached )   heating_valve_ON
        Vacuum_pump_ON
           }
    else{  if(PSU < -80) Vacuum_pump_OFF }

}

 void mythread::timeDone() //30s排1s
 {
     if(sterilization_protype == DRY_PROTYPE)
     {
         dry();
     }
     else
     {

        exhaust_valve_ON
        QTimer::singleShot(300, this, SLOT(timeDone2()));
     }
 }
 void mythread::timeDone2() //30s排1s
 {
   exhaust_valve_OFF
 }

 void mythread::calculate_F0()
 {
     F0+=pow(10.0,(TEM-121.0)/10.0)*0.1;
 }


 void mythread::stop()
 {
    sterilizing=0;
    if(this->isRunning()) stop_thread=1;
    if(timer->isActive()) timer->stop();
    if(sterilization_protype != LEAK_TEST_PROTYPE ) rec->save();
    pressure_balanced=0;
 }

  void mythread::delay_heating(int sec,bool condition)
 {
      if( this->condition=condition )
      {
      if( stype->is_pulsing_vacuum ) condition_psu=pulsing_highest_pressure;
      else condition_psu=cold_exhaust_pressure;
      }
     // QTimer::singleShot(10000, this, SLOT(condition_heating()));出错，疑太多静态函数调用冲突
    delay_heat_timer->start(sec*1000);
 }

   void mythread::condition_heating()
   {
       if( condition && PSU > condition_psu ) {return;}
        heating_valve_ON
   }

   void mythread::pressure_balance()
   {

           if(PSU<bPSU) { if(!hTem) heating_valve_ON}
           else if(PSU>bPSU) { if(!lTem)  heating_valve_OFF }
   }

   bool mythread::sterilization_process()

{

   emit set_status_display(2);  //     DISPLAY(2)
   timer->start(15000);
   exhaust_valve_OFF
   air_intake_valve_ON
   heating_valve_OFF
  // SLEEP(60)
   SLEEP_return_bool(60)
   pressure_balanced=1;
   heating_valve_ON
   //running_timer.start();
   while(TEM<sterilization_temperature-3)
   {    msleep(500); _EXIT_0 monitor_program->heat_slot();//heating_valve_ON
   }
   heating_valve_OFF
   timer->stop();
   air_intake_valve_ON ;
   emit set_count_down_timer(prebalance_time);
   for(int i=0; i< prebalance_time ; i++)
   {
   if(TEM< sterilization_temperature - 4)  { heating_valve_ON; exhaust_valve_OFF;}//
   else if(TEM> sterilization_temperature - 4) {heating_valve_OFF; exhaust_valve_ON }//
   _EXIT_0
   msleep(1000);
   }

   heating_valve_ON;
   air_intake_valve_ON;
   exhaust_valve_OFF;

   while(TEM<sterilization_temperature+0.5) { heating_valve_ON;msleep(500);_EXIT_0 }
   heating_valve_OFF
   bPSU=PSU+3;
   emit set_count_down_timer(sterilization_time);
   emit set_status_display(3);  //DISPLAY(3)

   timer->start(30000);
   int i=sterilization_time;
   bool over_temperature=0;
   while(i--)
   {

   if( TEM > sterilization_temperature+ high_balance_temperature) { heating_valve_OFF hTem=1;}
   else if( TEM <sterilization_temperature+ low_balance_temperature){heating_valve_ON lTem=1;}
   else {    hTem=0; lTem=0;}

   if(TEM > sterilization_temperature+ 2.5) {exhaust_valve_ON over_temperature=1;}
   else {if(over_temperature) {exhaust_valve_OFF over_temperature=0;}}

   if( whole_machine_parameter.temperature_control_type )//pressure_balance();
   {
            if(PSU<bPSU)
           {

          if(i>1)
          {
              msleep(995);
              i--;
              if(PSU<bPSU) { if(!hTem) heating_valve_ON}
          }
            }
            else if(PSU >bPSU)
            {
            if(i>1)
             {
              msleep(995);
              if(i>1) i--;
              if(PSU>bPSU) { if(!lTem)  heating_valve_OFF }
             }
            }
   }
   if(i<0) break;
   low_temperature_process();
   _EXIT_0
   msleep(990);
   }
   timer->stop();
   F0_timer->stop();
   pressure_balanced=0;
 //  heating_valve_OFF;
   air_intake_valve_OFF;
   exhaust_valve_ON;

   }
  /*
   void mythread::period_heat_timeout()
   {

       if(TEM>sterilization_temperature+ high_balance_temperature)   {period_heat_time++;}
       else {
           if(TEM<sterilization_temperature+ low_balance_temperature)  {period_heat_time--;}
            timing_heat(1000);
       }
       period_heat_timer->start(period_heat_time*1000);
   }

  void mythread::timing_heat(int ms)
  {
   heat_timer->start(ms);
   heating_valve_ON

  }

  void mythread::timing_heat_timeout()
  {
   heating_valve_OFF

  }
*/
